<template>
    <div class="login">
        <div class="left-side fl">
            <div class="login-module">
                <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" label-position="top">
                    <div class="login-font mt35">账号</div>
                    <el-form-item label="" prop="name">
                        <el-input v-model="ruleForm.name" placeholder=""></el-input>
                    </el-form-item>
                    <div class="login-font">密码</div>
                    <el-form-item label="" prop="password">
                        <el-input v-model="ruleForm.password" type="password" autocomplete="off"></el-input>
                    </el-form-item>
                    <div class="clearfix">
                        <el-link class="fl" :underline="false">忘记密码</el-link>
                        <el-link
                            type="primary"
                            class="fr"
                            :underline="false"
                            @click.stop="ToRegister"
                        >
                            注册   
                        </el-link>
                        <!-- <div class="fr" @click="ToRegister">注册</div> -->
                    </div>
                </el-form>
                <div class="mt20">
                    <el-button
                        type="primary"
                        class="login-button"
                        size="large"
                        @click="login"
                    >
                        登录
                    </el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { getUsername } from '@/api/api.query.util';
import type { FormInstance } from 'element-plus';
import { setCookie, deleteCookie, debounce } from '@/utils/utils'

const ruleFormRef = ref<FormInstance>();
const router = useRouter();
const ruleForm = reactive({
    name: '',
    password: '',
});
// 组件初始化
init();

function init() {
    clearToken();
}

function login() {
    debounce(() => {
        getUsername({
        method: 'post',
        data: {
            name: ruleForm.name,
            password: ruleForm.password
        }
    }).then((res) => {
        console.log(res)
        if(res.code === 200) {
            ElMessage({
                        message: '登录成功',
                        type: 'success',
                    });
            router.push( { name: '/home/service-list' })
            setCookie('userName', ruleForm.name)
        }
    })     
            }, 500);
}

function ToRegister() {
    router.push({ name: '/register'})
}

const rules = reactive({
    username: [
        {
            required: true,
            message: '账号不能为空',
            trigger: 'change',
        },
    ],
    password: [
        {
            required: true,
            message: '密码不能为空',
            trigger: 'change',
        },
    ],
});

/**
 * 清除 Token
 */
 function clearToken() {
    deleteCookie('userName');
}
</script>

<style scoped>
.login {
    height: 100%;
    background: url('../../assets/image/login-bg.svg');
}

.left-side {
    width: 50%;
    height: 100%;
    position: relative;
}

.clearfix:after {
    content: '';
    height: 0;
    line-height: 0;
    display: block;
    visibility: hidden;
    clear: both;
}

.clearfix {
    zoom: 1;
}

.fl {
    float: left;
}

.fr {
    float: right;
}

.mt35 {
    margin-top: 35px;
}

.mt20 {
    margin-top: 20px;
}

.login-module {
    position: absolute;
    top: 27%;
    left: 70%;
    width: 46%;
    margin: 0 auto;
}

.hy-font {
    font-size: 32px;
    color: #333333;
    line-height: 32px;
}

.login-font {
    font-size: 14px;
    line-height: 14px;
    display: inline-block;
    margin-bottom: 5px;
}

.login-input {
    height: 5%;
    width: 100%;
    display: block;
}

.login-button {
    width: 100%;
    color: #ffffff;
}
</style>
